#region 模块信息
// **********************************************************************
// 作者(Author):                  GUO
// 创建时间(CreateTime):           2019/3/22 13:28:19
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// UI模块基类
/// </summary>
public class UIModuleBase : MonoBehaviour
{
    protected Dictionary<string, ModelDispatcher.Handler> m_Dic;

    public virtual Dictionary<string, ModelDispatcher.Handler> DicNotificationInterests()
    {
        return new Dictionary<string, DispatcherBase<ModelDispatcher, TransferData, string>.Handler>();
    }

    void Awake()
    {
        m_Dic = DicNotificationInterests();
        foreach (KeyValuePair<string, DispatcherBase<ModelDispatcher, TransferData, string>.Handler> pair in m_Dic)
        {
            ModelDispatcher.Instance.AddEventListener(pair.Key,pair.Value);
        }
        OnAwake();
    }

    void Start()
    {
        OnStart();
    }

    void Update()
    {
        OnUpdate();
    }

    void OnDestroy()
    {
        OnDestroy();
    }
    protected virtual void OnAwake() { }
    protected virtual void OnStart() { }
    protected virtual void OnUpdate() { }
    protected virtual void BeforeDestroy() { }
    protected void AddListener(string notificationName,ModelDispatcher.Handler action)
    {
        if (m_Dic == null)
        {
            m_Dic=new Dictionary<string, DispatcherBase<ModelDispatcher, TransferData, string>.Handler>();
        }
        if (m_Dic.ContainsKey(notificationName)) return;
        m_Dic.Add(notificationName,action);
        ModelDispatcher.Instance.AddEventListener(notificationName,action);
    }

    protected void RemoveListener(string notificationName, ModelDispatcher.Handler action)
    {
        if (m_Dic == null) return;
        if(!m_Dic.ContainsKey(notificationName))return;
        m_Dic.Remove(notificationName);
        ModelDispatcher.Instance.RemoveEventListener(notificationName,action);
    }
}
